package cn.hdmis.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.Action;

import cn.hdmis.bean.QueryResult;
import cn.hdmis.entity.Grade;
import cn.hdmis.entity.Guardian;
import cn.hdmis.entity.Student;
import cn.hdmis.entity.StudentGrade;
import cn.hdmis.service.GradeService;
import cn.hdmis.service.GuardianService;
import cn.hdmis.service.StudentService;
import cn.hdmis.shiro.SecurityUtils;

public class StuentAction extends BaseAction {
	
	private static final long serialVersionUID = 8730438652362564233L;
	
	private Student  stu;
	private Guardian gua;
	private List<Guardian> guaList;
	private List<Student> studentList;
	private StudentService studentService;
	private GuardianService guardianService;
	private GradeService gradeService;
	
	public Student getStu() {
		return stu;
	}
	public void setStu(Student stu) {
		this.stu = stu;
	}
	public Guardian getGua() {
		return gua;
	}
	
	public void setGua(Guardian gua) {
		this.gua = gua;
	}
	
	public void setGuaList(List<Guardian> guaList) {
		this.guaList = guaList;
	}
	public List<Guardian> getGuaList() {
		return guaList;
	}
	@JSON(name="rows")
	public List<Student> getStudentList() {
		return studentList;
	}
	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
	@JSON(serialize=false)
	public StudentService getStudentService() {
		return studentService;
	}
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	@JSON(serialize=false)
	public GuardianService getGuardianService() {
		return guardianService;
	}
	public void setGuardianService(GuardianService guardianService) {
		this.guardianService = guardianService;
	}
	@JSON(serialize=false)
	public GradeService getGradeService() {
		return gradeService;
	}
	public void setGradeService(GradeService gradeService) {
		this.gradeService = gradeService;
	}
	public String addStudent() {
		stu.setRegdate(new Date());
		try {
			this.studentService.saveStudent(stu);
		} catch (Exception e) {
			e.printStackTrace();
			setMSG(MSG_FAILED);
		}
		return "addSuccess";
	}
	@SuppressWarnings("unchecked")
	public String loadAllStudent() {
		QueryResult result  = null;
		initPage();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(stu!=null){
			paramMap.put("id",stu.getId());
			paramMap.put("name", stu.getName());
			paramMap.put("studentType",stu.getStudentType());
		}
		try {
			result = studentService.getAllStudent();
			List<Student> ls = result.getResultList();
			this.setStudentList( ls );
			pageBean.setTotalCount(result.getTotalRecord());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "studentList";
	}
	
	public String remove(){
		String userId = this.getRequest().getParameter("id");
		Long id = Long.valueOf(userId);
		try {
			this.studentService.delStudent(studentService.getStudent(id));
		} catch (Exception e) {
			setMSG(MSG_FAILED);
		}
		return "remove";
	}
	
	public String removeMore(){
		String userId = this.getRequest().getParameter("ids");
		String[] idArry =  userId.split(",");
		List<Serializable> idList = new ArrayList<Serializable>();
		if(idArry.length>0){
			for (String id : idArry) {
				idList.add(Long.valueOf(id));
			}
		}
		try {
			this.studentService.delStudents(idList);
		} catch (Exception e) {
			logger.error(e.getMessage());
			setMSG(MSG_FAILED);
		}
		return "remove";
	}
	
	public String viewStudent(){
		String userId = this.getRequest().getParameter("user_id");
		if(userId!=null&&!userId.trim().isEmpty()){
			Long id = Long.valueOf(userId);
			this.setStu(studentService.getStudent(id));
		}
		return "view";
	}
	
	public String iniSelectGrade(){
		String userId = this.getRequest().getParameter("user_id");
		if(userId!=null&&!userId.trim().isEmpty()){
			Long id = Long.valueOf(userId);
			this.setStu(studentService.getStudent(id));
		}
		List<Grade> list = this.gradeService.getAllGrade();
		this.getRequest().setAttribute("gradeList", list);
		this.getRequest().setAttribute("userId", userId);
		return "iniSelectGrade";
	}
	
	public String selectGrade(){
		String personId = this.getRequest().getParameter("personId");
		String gradeId = this.getRequest().getParameter("gradeId");
		try {
			this.studentService.updateGrade(Long.valueOf(personId),Long.valueOf(gradeId));
		} catch (Exception e) {
			e.printStackTrace();
			setMSG(MSG_FAILED);
		}
		return "selectGrade";
	}
}
